<template>
  <div class="app-container">
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="课 程 名">
              <el-input style="width: 95%" v-model="searchObj.courseName" placeholder="课程名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()"
                     :disabled="$hasBP('bnt.course.list') === false">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData"
                     :disabled="$hasBP('bnt.course.list') === false">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add"
                 :disabled="$hasBP('bnt.course.add') === false">添 加</el-button>
    </div>

    <!-- 列表 -->
    <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%; margin-top: 10px">
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="courseName" label="课程名" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="storeName" label="课程门店" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="coachName" label="课程教练" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="startTime" label="开始时间" align="center" width="160" />
      <el-table-column prop="endTime" label="结束时间" align="center" width="160" />
      <el-table-column prop="max" label="最大人数" align="center" width="100" />
      <el-table-column prop="price" label="价格" align="center" width="100" />
      <el-table-column prop="vipPrice" label="会员价格" align="center" width="100" />
      <el-table-column label="课程状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status===0" type="">未发布</el-tag>
          <el-tag v-else-if="scope.row.status===1" type="success">可预约</el-tag>
          <el-tag v-else-if="scope.row.status===2" type="warning">已开始停止预约</el-tag>
          <el-tag v-else-if="scope.row.status===3" type="danger">已结束</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"
                     :disabled="$hasBP('bnt.course.update') === false" />
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)"
                     title="删除" :disabled="$hasBP('bnt.course.remove') === false" />
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handlePublish(scope.row)"
            v-if="scope.row.status===0"
          >发布</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination :current-page="page" :total="total" :page-size="limit"
                   style="padding: 30px 0; text-align: center" layout="total, prev, pager, next, jumper"
                   @current-change="fetchData" />
  </div>
</template>

<script>
import courseApi from "@/api/content/course";
import {Message, MessageBox} from "element-ui";

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      courses: [], // 门店列表

      createTimes: [],

      // dialogVisible: false,
      // sysUser: defaultForm,
      // saveBtnDisabled: false,
    };
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log("list created......");
    this.fetchData();
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log("list mounted......");
  },

  methods: {
    // 加载课程列表数据
    fetchData(page = 1) {
      this.page = page;
      courseApi
        .getPageList(this.page, this.limit, this.searchObj)
        .then((response) => {
          this.list = response.data.records;
          console.log(response.data)
          this.total = response.data.total;
          // 数据加载并绑定成功
          this.listLoading = false;
        });
    },
    confirm(content) {
      return MessageBox.confirm(content, "系统提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      })
    },
    msgError(content) {
      Message.error(content)
    },
    // 成功消息
    msgSuccess(content) {
      Message.success(content)
    },
    //开始课程
    /** 发布按钮操作 */
    handlePublish(row) {
      console.log(row);
      if (new Date() > new Date(row.startTime)) {
        this.msgError("预约开始时间已过，请修改时间后再发布");
        return;
      }
      this.confirm('是否确认发布课程名称为"' + row.courseName + '"的数据项？').then(() => {
        return courseApi.publishCourse(row.id);
      }).then((res) => {
        console.log(res);
        this.fetchData(1);
        this.msgSuccess("课程已发布");
      }).catch(() => {});
    },

    // 重置查询表单
    resetData() {
      console.log("重置查询表单");
      this.searchObj = {};
      this.createTimes = [];
      this.fetchData();
    },

    // 根据id删除数据
    removeDataById(id) {
      // debugger
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // promise
          // 点击确定，远程调用ajax
          return courseApi.removeById(id);
        })
        .then((response) => {
          this.fetchData(this.page);
          this.$message.success(response.message || "删除成功");
        })
        .catch(() => {
          this.$message.info("取消删除");
        });
    },



    //去添加
    add() {
      this.$router.push("/content/courseInfo");
    },
    //去编辑
    edit(id) {
      this.$router.push({
        path: "/content/courseInfo",
        query: { id: id },
      });
    },
  },
};
</script>
